<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课件名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入课件名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.course_manage" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['course:course_ware:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['course:course_ware:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="course_wareList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed align="center" width="75">
        <template slot-scope="scope">
          <el-button size="medium" icon="el-icon-video-play" @click="openVideo(scope.row)" circle></el-button>
        </template>
      </el-table-column>
      <el-table-column label="缩略图" align="center" width="90" prop="thumbnail">
        <template slot-scope="scope">
<!--          <video class="videoImg" width="60" height="60" preload="meta" style="pointer-events: none" controls>-->
<!--            <source :src="scope.row.video">-->
<!--          </video>-->
          <viewer>
            <el-avatar shape="square" size="large" :src="scope.row.thumbnail"></el-avatar>
          </viewer>

        </template>
      </el-table-column>
      <el-table-column label="课件名称" align="center" prop="name"/>
<!--      <el-table-column label="视频" align="center" prop="video"/>-->
      <el-table-column label="标签" align="center" prop="reservedOne">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reservedOne === '1'"><i class="el-icon-ice-drink"></i>  婴儿护理</el-tag>
          <el-tag v-if="scope.row.reservedOne === '2'" type="success"><i class="el-icon-reading"></i>  访视礼仪</el-tag>
          <el-tag v-if="scope.row.reservedOne === '3'" type="info"><i class="el-icon-s-order"></i>   沟通技巧</el-tag>
          <el-tag v-if="scope.row.reservedOne === '4'" type="warning"><i class="el-icon-first-aid-kit"></i>  产妇护理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="reservedTwo"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_manage" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '0'" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['course:course_ware:edit']">修改</el-button>
          <el-button v-if="scope.row.status == '0'" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['course:course_ware:remove']">删除</el-button>
          <el-button size="mini" type="text" @click="manageUpdate(scope.row)" v-hasPermi="['course:course_train:edit']"
                     :icon="scope.row.status == '0' ? 'el-icon-upload2' : 'el-icon-download'">
            {{scope.row.status == '0' ? '上架' : '下架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改培训课件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :destroy-on-close="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="视频" prop="video">
<!--          <el-input v-model="form.video" placeholder="请输入视频"/>-->
          <el-upload v-loading="loading" class="upload-demo" drag :on-change="fileChange" action="#" :auto-upload="false" multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
          <el-tag v-if="form.video == undefined ||form.video == null || form.video ==''" type="danger">未上传</el-tag>
          <el-tag v-else type="success">已上传 时长：{{form.duration}}</el-tag>
        </el-form-item>
        <el-form-item label="课件名称" prop="name">
          <el-input v-model="form.name" style="width: 360px" placeholder="请输入课件名称"/>
        </el-form-item>
        <el-form-item label="标签" prop="reservedOne">
          <el-radio-group v-model="form.reservedOne">
            <el-radio-button label="1"><i class="el-icon-ice-drink"></i>   婴儿护理</el-radio-button>
            <el-radio-button label="2"><i class="el-icon-reading"></i>   访视礼仪</el-radio-button>
            <el-radio-button label="3"><i class="el-icon-s-order"></i>   沟通技巧</el-radio-button>
            <el-radio-button label="4"><i class="el-icon-first-aid-kit"></i>   产妇护理</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" prop="reservedTwo">
          <el-input type="textarea" :rows="6" style="width: 360px" v-model="form.reservedTwo" placeholder="请输入描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 播放视频对话框 -->
    <el-dialog :title="videoName" :visible.sync="videoOpen" append-to-body center fullscreen :destroy-on-close="true">
      <div class="myVideoClass">
        <video-player class="video-player vjs-custom-skin" ref="videoPlayer" :playsinline="true" :options="videoPlayOptions"/>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listCourse_ware,
  delCourse_ware,
  addCourse_ware,
  updateCourse_ware, upload
} from "@/api/course/course_ware";

export default {
  name: "Course_ware",
  dicts: ['course_manage'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训课件表格数据
      course_wareList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sectionId: null,
        name: null,
        openType: 1,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "课件名称不能为空", trigger: "blur"}
        ],
        video: [
          {required: true, message: "视频不能为空", trigger: "blur"}
        ],
        reservedOne: [
          {required: true, message: "请选择标签", trigger: "blur"}
        ]
      },
      videoOpen:false,
      videoName:'',
      //视频播放
      videoPlayOptions : {
        playbackRates: [0.5, 1.0, 1.5, 2.0], //播放速度
        autoplay: false, //如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          type: "video/mp4",
          src: "" //url地址
        }],
        poster: "", //你的封面地址
        // width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放，请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true  //全屏按钮
        }
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训课件列表 */
    getList() {
      this.loading = true;
      this.queryParams.openType = 1;
      listCourse_ware(this.queryParams).then(response => {
        this.course_wareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        deptId: null,
        sectionId: null,
        name: null,
        thumbnail: null,
        video: null,
        duration: null,
        openType: 1,
        status: null,
        reservedOne: null,
        reservedTwo: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训课件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row};
      this.open = true;
      this.title = "修改培训课件";
      // getCourse_ware(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改培训课件";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse_ware(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.openType = 1 ;
            addCourse_ware(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训课件编号为"' + ids + '"的数据项？').then(function () {
        return delCourse_ware(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/course_ware/export', {
        ...this.queryParams
      }, `course_ware_${new Date().getTime()}.xlsx`)
    },
    /** 上架或者下架 */
    manageUpdate(row){
      var body = {};
      body.id = row.id;
      body.status = row.status == '0' ? '1' : '0';
      updateCourse_ware(body).then(response => {
        this.$modal.msgSuccess((row.status == '0' ? '上架' : '下架') +"成功");
        this.getList();
      });
    },
    //上传文件方法
    fileChange(file,fileList){
      this.loading = true
      //上传文件，获取地址
      upload(file).then(response => {
        this.form.video = response.msg;
        this.form.thumbnail = response.thumbnail;
        this.form.duration = response.duration;
        fileList.splice(1);
        this.loading = false;
      });
    },
    //打开视频
    openVideo(row){
      this.videoPlayOptions['sources'][0].src = row.video;
      this.videoName = row.name;
      this.videoOpen = true;
      this.$forceUpdate();
      // //查询课件
      // getCourse_ware(row.id).then(response => {
      //   //this.video.url = row.video;
      //
      // });
    },
  }
};
</script>
<style>
.myVideoClass{
  object-fit: fill;
  margin: 0 auto;
  width: 1500px;
}
</style>
